#include "ProductTestHandle.h"
#include "ProductTestKey.h"
#include "ProductTestUartDataProcess.h"
#include "ProductTestIotSpkPlay.h"

#define DEF_TAG         "TutkHandle"

#define DEF_BURN_UID_STR_RET "{\"ap_name\":\"%s\", \"version\":\"%s\",\"wifi\":\"%s\",\"video\": \"%s\", \"tfcard\":\"%s\", \"serial\":\"%s\", \"key\":\"%s\"}"

extern Com_s32 g_Tutk_ucheck_flag;

extern char g_TUTKSdk_UID[DEF_COM_UUID_LEN];

int Handle_IOTYPE_USER_DEVINFO(int av_index, char* buf)
{
    SMsgAVIoctrlDeviceInfoResp info;
    struct statfs sd_fs;

    memset(&info,0,sizeof(info));

    if (statfs("/mnt/config/record", &sd_fs) != 0)
    {
        PRT_ERR(DEF_TAG,"statfs failed!\n");
    }
    else
    {
        info.total = ((Com_u64)sd_fs.f_blocks * (Com_u64)sd_fs.f_bsize) >> 10;
        info.free = ((Com_u64)sd_fs.f_bavail * (Com_u64)sd_fs.f_bsize) >> 10;
    }

    avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_DEVINFO_RESP,
                 (char *)&info,sizeof(SMsgAVIoctrlDeviceInfoResp));
    return 0;
}

int Handle_IOTYPE_USER_GET_LISTWIFIAP(int av_index, char* buf)
{
    Com_s32 size;
    Com_s8 wifi_rssi[DEF_COM_WIFI_RSSI_LEN] = {0};
    SMsgAVIoctrlListWifiApResp resp;

    memset(&resp, 0, sizeof(resp));

    size = sizeof(SMsgAVIoctrlListWifiApResp) + sizeof(SWifiAp) * 2;
    Ext_InSdkGetInfo_WiFiRssi(wifi_rssi);
    Ext_InSdkGetInfo_WiFiEssid(resp.stWifiAp[1].ssid);
    Ext_ProductTest_Get_WiFi_SSID(resp.stWifiAp[0].ssid);
    if(strcmp(resp.stWifiAp[1].ssid,resp.stWifiAp[0].ssid) == 0)
    {
        resp.number = 1;
    }
    else
    {
        resp.number = 2;
        resp.stWifiAp[1].signal = atoi(wifi_rssi) + 110;
    }

    PRT_DBG(DEF_TAG,"wifi list number: %d\n",resp.number);
    resp.stWifiAp[0].signal = atoi(wifi_rssi) + 110;
    avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_LISTWIFIAP_RESP,(char *)&resp,size);
    return 0;
}

int Handle_IOTYPE_USER_GET_PRODUT_AUTOTEST_RESULT(int av_index, char* buf)
{
    Com_s8 video[4] = "ok";
    Com_s8 wifi_signal[64] = {0};
    Com_s8 all_version[128] = {0};
    Com_s8 key[PRODUCT_TEST_RESULT_LEN]= {0};
    Com_s8 uart[PRODUCT_TEST_RESULT_LEN] = {0};
    Com_s8 tfcard[PRODUCT_TEST_RESULT_LEN] = {0};
    Com_s8 ap_name[PRODUCT_TEST_PRODUCT_MODE_LEN] = {0};
    Com_s8 wifi_ssid[PRODUCT_TEST_WIFI_SSID_LEN] = {0};
    Com_s8 version[PRODUCT_TEST_VERSION_LEN] = {0};
    Com_s8 mcu_ver[MCU_VERSION_LEN] = {0};
    Com_s8 wifi_rssi[DEF_COM_WIFI_RSSI_LEN] = {0};

    SmsgAVIoctrlGetProductTestResultReq *req = (SmsgAVIoctrlGetProductTestResultReq*)buf;
    SmsgAVIoctrlGetProductTestResultResp resp;
    memset(&resp, 0, sizeof(resp));

    resp.command_type = req->command_type;
    resp.result = 0;

    Ext_ProductTest_Get_WiFi_SSID(wifi_ssid);
    Ext_InSdkGetInfo_WiFiRssi(wifi_rssi);
    Ext_ProductTest_Get_Version(version);
    Ext_ProductTest_Get_SD_Result(tfcard);
    strcpy(key,Ext_ProductTest_Key_Get_Result());
    strcpy(uart,Ext_ProductTest_Uart_Result());
    strcpy(mcu_ver,Ext_ProductTest_Uart_Get_Version());

    if(Ext_InComFile_Access(DEF_WIFI_AP_NAME) == DEF_COM_FILE_EXIST)
    {
        Ext_InComFile_ReadStr(DEF_WIFI_AP_NAME,ap_name,sizeof(ap_name));
    }
    else
    {
        strcpy(ap_name,"SmartLife_");
    }

    sprintf(all_version,"%s-%s",version,mcu_ver);

    sprintf(wifi_signal,"%s:%s",wifi_ssid,wifi_rssi);
    sprintf(resp.result_info, DEF_BURN_UID_STR_RET, ap_name, all_version, wifi_signal,video,tfcard,uart,key);
    resp.result_info_len = strlen(resp.result_info);

    PRT_INFO(DEF_TAG,"info:%s\n", resp.result_info);
    PRT_NORMAL("req max command_type num:%d, resp value :%d, result_info_len:%d\n",
                  req->command_type, resp.value, resp.result_info_len);

    if(avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_GET_PRODUT_AUTOTEST_RESULT_RESP,(char *)&resp, sizeof(resp)) < 0)
    {
        return -1;
    }

    return 0;
}

int Handle_IOTYPE_USER_SET_PRODUCT_INFO(int av_index, char* buf)
{
    SmsgAVIoctrlSetProductInfoReq *req = (SmsgAVIoctrlSetProductInfoReq*)buf;
    SmsgAVIoctrlSetProductInfoResp resp;
    memset(&resp, 0, sizeof(resp));

    resp.command_type = req->command_type;
    resp.result = 0;
    resp.value = req->value;

    switch(req->command_type)
    {
    case SET_PRODUCT_IRCUT_IRLED_VALUE :
    {
        /* night mode */
        if(req->value == 1)
        {
            Ext_SdkNightMonitor_SwitchMode(DEF_COM_TRUE);
            Ext_SdkNightMonitor_SetWorkMode(NIGHT_MODE_ON);
        }
        else /* day mode */
        {
            Ext_SdkNightMonitor_SwitchMode(DEF_COM_TRUE);
            Ext_SdkNightMonitor_SetWorkMode(NIGHT_MODE_OFF);
        }
    }
        break;
    case SET_PRODUCT_CLEAN_UID_LINK_WIFI:
    {
        /* 产测app勾选选项是0，表示清除标志位，下次启动继续进入产测模式 */
        if (req->value == 0)
        {
            printf("next time will go to product mode.\n");
            resp.result = E_DNS_TEST_SHELL_CLEAN_RET;
        }
        else
        {
            resp.result = E_DNS_TEST_SHELL_CLEAN_RET;
        }
    }
        break;

    default:
        break;
    }

    printf("resp command_type :%d, resp value :%d, result:%d\n",
           resp.command_type, resp.value, resp.result);

    if(avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_SET_PRODUT_INFO_RESP,(char *)&resp, sizeof(resp)) < 0)
    {
        return -1;
    }

    return 0;
}

int Handle_IOTYPE_USER_GET_MAC(int av_index, char* buf)
{
    int ret;
    SmsgAVIoctrlGetDevMacResp resp;

    memset(&resp, 0, sizeof(resp));

    resp.result = 0;

    ret = Ext_InSdkGetInfo_HardwareMac(resp.mac,MAC_SRC);
    if(ret != 0)
    {
        resp.result = -1;
    }

    printf("resp result: %d,mac: %s\n", resp.result,resp.mac);
    if(avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_GET_DEV_MAC_RESP,(char *)&resp, sizeof(resp)) < 0)
    {
        return -1;
    }

    return 0;
}

int Handle_IOTYPE_USER_GET_PID(int av_index, char* buf)
{
    int ret;
    SmsgAVIoctrlGetPIDResp resp;

    printf("upload device PID\n");
    memset(&resp,0,sizeof(resp));

    ret = Ext_InSdkGetInfo_Pid(resp.PID);
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"PID is null\n");
        strcpy(resp.PID,"1234567890");
    }

    avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_GET_PID_RESP,
                 (char *)&resp, sizeof(SmsgAVIoctrlGetPIDResp));
    return 0;
}

int Handle_IOTYPE_USER_SET_PETWANT_TYPE(int av_index, char* buf)
{
    return 0;
}

int Handle_IOTYPE_USER_SET_LOCATION_MODE_TYPE(int av_index, char* buf)
{
    SmsgAVIoctrlSetLocationModeReq *req = (SmsgAVIoctrlSetLocationModeReq*)buf;
    SmsgAVIoctrlSetLocationModeResp resp;

    memset(&resp, 0, sizeof(resp));
    resp.result = -1;

    if(avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_SET_LOCATION_MODE_RESP,(char *)&resp, sizeof(resp)) < 0)
    {
        return -1;
    }
    PRT_NORMAL("location mode result: %d,mode: %d\n",resp.result,req->mode);

    return 0;
}

int Handle_IOTYPE_USER_GET_DEV_UCHECK(int av_index, char* buf)
{
    SmsgAVIoctrlGetDevUcheckResp resp;

    memset(&resp, 0, sizeof(resp));

    resp.result = 0;
    resp.ucheck = g_Tutk_ucheck_flag;
    strcpy(resp.uid,g_TUTKSdk_UID);

    PRT_INFO(DEF_TAG,"result: %d,uid: %s,mac: %s\n",resp.result,resp.uid,resp.mac);
    if(avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_GET_DEV_UCHECK_RESP,(char *)&resp,sizeof(resp)) < 0)
    {
        return -1;
    }

    return 0;
}

int Handle_IOTYPE_USER_SET_DEV_PID(int av_index, char* buf)
{
    Com_s32 ret;
    Com_s8 cmd[48];
    SmsgAVIoctrISetDevPidReq *req = (SmsgAVIoctrISetDevPidReq*)buf;
    SmsgAVIoctrlSetDevPidResp resp;

    memset(cmd, 0, sizeof(cmd));
    memset(&resp, 0, sizeof(resp));

    resp.result = 0;
    sprintf(cmd,"oem_tuya_pid.sh %s",req->pid);
    Ext_InSdkTcpSystem_Cmd(cmd);

    ret = Ext_InSdkGetInfo_Pid(resp.pid);
    if(ret != DEF_COM_SUCCESS)
    {
        resp.result = -1;
        PRT_ERR(DEF_TAG,"PID is null\n");
        strcpy(resp.pid,"1234567890");
    }

    PRT_INFO(DEF_TAG,"%s result: %d,req->pid: %s,resp.pid: %s\n",__FUNCTION__,resp.result,req->pid,resp.pid);
    if(avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_SET_DEV_PID_RESP,(char *)&resp,sizeof(resp)) < 0)
    {
        return -1;
    }

    return 0;
}

int Handle_IOTYPE_USER_DEVICE_RESET(int sid, int av_index, char* buf)
{
    SMsgAVIoctrlSetDevResetResp resp;

    memset(&resp,0,sizeof(resp));

    Ext_ProductTest_UartHandle_Reset_MCU();
    resp.result = 0;
    avSendIOCtrl(av_index, IOTYPE_USER_IPCAM_DEVICE_RESET_RESP,(char *)&resp,sizeof(resp));

    StProductTestSpkInfo info;

    info.path = DEF_WAV_DING_DONG;
    info.cnt = 1;
    Ext_ProductTest_Spk_WavPlay_Init(&info);
    static Com_u8 app_reset_flag = DEF_COM_FALSE;
    if(app_reset_flag == DEF_COM_FALSE)
    {
        app_reset_flag = DEF_COM_TRUE;
        /** 还原出厂设置 **/
        Ext_ProductTest_Key_Restore_Factory();
        sleep(2);
        Ext_InSdkTcpSystem_Cmd("skip_product_test.sh");
        sleep(10);
    }

    return 0;
}


int Handle_IOTYPE_USER_SKIP_PRODUCT_TEST(int av_index, char* buf)
{
    SmsgAVIoctrlSkipProductTestResp resp;

    memset(&resp,0,sizeof(resp));

    resp.result = 0;
    Ext_InComFile_JudgeCreat(DEF_FLAG_SKIP_HARDWARE_TEST);
    PRT_INFO(DEF_TAG,"creat skip test flag\n");
    if(Ext_InComFile_Access(DEF_FLAG_SKIP_HARDWARE_TEST) == DEF_COM_FILE_NOT_EXIST)
    {
        PRT_ERR(DEF_TAG,"creat skip test flag failed\n");
        resp.result = -1;
    }

    avSendIOCtrl(av_index,IOTYPE_USER_IPCAM_SKIP_PRODUCT_TEST_RESP,(char *)&resp,sizeof(resp));
    return 0;
}

int Handle_IOTYPE_USER_GET_BLE_MCU_MAC(int av_index, char* buf)
{
    SmsgAVIoctrlGetBleMcuMacResp resp;

    memset(&resp,0,sizeof(resp));
    resp.result = 0;

    Ext_ProductTest_Get_MCU_Mac(resp.mac);

    avSendIOCtrl(av_index,IOTYPE_USER_IPCAM_GET_BLE_MAC_RESP,(char *)&resp,sizeof(resp));
    return 0;
}

int Handle_IOTYPE_SET_DEVICE_BASIC_CONFIG(int av_index, char* buf)
{
    SSetDeviceBasicConfigReq *req = (SSetDeviceBasicConfigReq *)buf;
    SSetDeviceBasicConfigResp resp;

    memset(&resp,0,sizeof(resp));
    resp.result = 0;
    resp.val = req->val;
    resp.type = req->type;

    Ext_InSdkIniBasic_SetVal(req->type,req->val);
    Ext_InSdkIniBasic_GetAll();
    if(Ext_InSdkIniBasic_GetVal(req->type) != req->val)
    {
        resp.result = -1;
    }
    else
    {
        Ext_ProductTest_Change_BasicConfig(req->type,req->val);
    }

    PRT_INFO(DEF_TAG,"basic config: %d,type: %d,val: %d\n",resp.result,resp.type,resp.val);
    avSendIOCtrl(av_index,IOTYPE_SET_DEVICE_BASIC_CONFIG_RESP,(char *)&resp,sizeof(resp));
    return 0;
}

int Handle_IOTYPE_SET_TY_AP_NAME(int av_index, char* buf)
{
    SmsgAVIoctrlSetTyAPNameReq *req = (SmsgAVIoctrlSetTyAPNameReq *)buf;
    SmsgAVIoctrlSetTyAPNameResp resp;

    memset(&resp,0,sizeof(resp));
    resp.result = -1;

    Ext_InComFile_Write(DEF_WIFI_AP_NAME,req->ap_name,sizeof(req->ap_name));

    if(Ext_InComFile_Access(DEF_WIFI_AP_NAME) == DEF_COM_FILE_EXIST)
    {
        Ext_InComFile_ReadStr(DEF_WIFI_AP_NAME,resp.ap_name,sizeof(resp.ap_name));
        if(strcmp(resp.ap_name,req->ap_name) == 0)
        {
            resp.result = 0;
        }
    }

    PRT_INFO(DEF_TAG,"req->ap_name: %s,resp.ap_name: %s\n",req->ap_name,resp.ap_name);
    avSendIOCtrl(av_index,IOTYPE_USER_IPCAM_SET_TY_AP_NAME_RESP,(char *)&resp,sizeof(resp));
    return 0;
}

int Handle_IOTYPE_REPORT_UID_AUTHKEY(int av_index, char* buf)
{
    SmsgAVIoctrlReportUidAuthKeyResp resp;

    memset(&resp,0,sizeof(resp));

    resp.result = 0;
    strncpy(resp.uid,g_TUTKSdk_UID,sizeof(resp.uid));
    Ext_InSdkGetInfo_Authkey(resp.authkey);
    PRT_INFO(DEF_TAG,"uid: %s,authkey: %s\n",resp.uid,resp.authkey);
    avSendIOCtrl(av_index,IOTYPE_USER_IPCAM_REPORT_UID_AUTHKEY_RESP,(char *)&resp,sizeof(resp));
    return 0;
}

int Handle_IOTYPE_PRODUCT_FUNC_MODIFIED(int av_index, char* buf)
{
    SmsgProductFuncModifiedResp resp;
    SmsgProductFuncModifiedReq *req = (SmsgProductFuncModifiedReq *)buf;

    memset(&resp,0,sizeof(resp));

    resp.result = 0;
    resp.num = req->num;
    strcpy(resp.val,req->val);

    Ext_InSdkTcpSystem_Cmd("mkdir /mnt/config/product_func");
    Ext_InSdkTcpSystem_Cmd("rm /mnt/config/product_func/*");
    int int_val = atoi(req->val);
    if(req->num == 1)
    {
        if(int_val & PRO_FUNC_INCREASE_RECORD_MIC_VOLUME)
        {
            PRT_INFO(DEF_TAG,"INCREASE RECORD MIC VOLUME\n");
        }
    }

    avSendIOCtrl(av_index,IOTYPE_PRODUCT_FUNC_MODIFIED_RESP,(char *)&resp,sizeof(resp));
    return 0;
}

int Handle_IOTYPE_GET_BATTERY_CAPACITY(int av_index, char* buf)
{
    return 0;
}
